<?php
class HoadonnhapsController extends AppController {

	var $name = 'Hoadonnhaps';
	var $uses = array('Hoadonnhap','Nhanvien');
	function index() {
		$this->Hoadonnhap->recursive = 0;
		$this->set('hoadonnhaps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid hoadonnhap', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('hoadonnhap', $this->Hoadonnhap->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Hoadonnhap->create();
			if ($this->Hoadonnhap->save($this->data)) {
				$this->Session->setFlash(__('The hoadonnhap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The hoadonnhap could not be saved. Please, try again.', true));
			}
		}
		$nhanviens = $this->Hoadonnhap->Nhanvien->find('list');
		$this->set(compact('nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid hoadonnhap', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Hoadonnhap->save($this->data)) {
				$this->Session->setFlash(__('The hoadonnhap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The hoadonnhap could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Hoadonnhap->read(null, $id);
		}
		$nhanviens = $this->Hoadonnhap->Nhanvien->find('list');
		$this->set(compact('nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for hoadonnhap', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Hoadonnhap->delete($id)) {
			$this->Session->setFlash(__('Hoadonnhap deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Hoadonnhap was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function themhoadonnhap()
	{
		if(!empty($this->data))
		{
			$this->Hoadonnhap->create();
			$this->data['Hoadonnhap']['ngaynhap'] =date('Y-m-d',strtotime($this->data['Hoadonnhap']['ngaynhap'])); 
			if($this->Hoadonnhap->save($this->data))
			{
				$this->Session->setFlash(__('Đã thêm hóa đơn', true));
				$this->redirect(array('controller'=>'chitietnhaps','action'=>'dausachnhap',$this->Hoadonnhap->id));
			}
			else
			{
				$this->Session->setFlash(__('Lưu không thành công. Xin vui lòng thử lại', true));
			}
		}
		$this->Nhanvien->virtualFields = array('nhanvien' =>"CONCAT(masonv,' - ',tennv)");
		$nhanviens = $this->Nhanvien->find('list',array('fields'=>array('nhanvien')));
		$this->set(compact('nhanviens'));
	}
}
